
public class BinarySearch {
	public static int search(int[] ar, int target) {
		int low = 0;
		int high = ar.length - 1;

		//ヘッダ表示
		NumbersUtil.displayHeader(ar);
		while (low <= high) {
			int mid = calculateMidpoint(low, high);
			int midVal = ar[mid];
			
			if (midVal < target) {
				//探索内容を表示する
				NumbersUtil.displaySearching(ar, low, high, mid);
				low = mid + 1;
			}else if(midVal > target){
				//探索内容を表示する
				NumbersUtil.displaySearching(ar, low, high, mid);
				high = mid - 1;
			}else{
				return mid;
			}
		}

		return -1;
	}
	
	public static int calculateMidpoint(int low, int high){
		return (low + high) >>> 1;
	}
	
	
}
